import axios from "axios";
import eventLoop from "./event_loop";
import env from '@/assets/env'
import md5 from "./md5"

class Ajax{
    constructor(){
        // this.host = process.env.VUE_APP_HOST;
        // console.log(process)
        //  this.host = "http://gateway.wsdchigh.top";
        //this.host = env.host;
    }

    setUser(user){
        this.user = user;
        console.log("设置用户")
    }

    setVue(vue){
        this.vue = vue;
    }
    /**
     *  get请求
     *  @param  url     请求的地址
     *  @param  data    请求的数据
     *  @param  eventName   请求成功或者失败后 发送事件的事件名字
     *  使用解耦的方式，避开组件在声明周期之外可以监听到这里的结果
     */
    get(url,data,eventName){
        let that = this;
        axios.request({
            url:url.startsWith("http") ? url : this.host+url,
            method:"GET",
            data:data
        })
            .then(result => {
                //debugger
                eventLoop.publishEvent({
                    type:eventName,
                    status:'success',
                    data:result
                });

                if(that.vue && false){
                    that.vue.$aShow(result.data.msg);
                }
            })
            .catch(e => {
                console.log(e);
                eventLoop.publishEvent({
                    type:eventName,
                    status:"error",
                    data:e
                });

                if(that.vue){
                    that.vue.$aShow(e);
                }
            });
    }

    getWithToken(url,data,eventName){
        let that = this;
        if(this.user == null || this.user.accessToken == null){
            eventLoop.publishEvent({
                type:eventName,
                code:-3,
                msg:"用户未登录",
                data:{}
            });
            return ;
        }
        axios.request({
            url:url,
            method:"get",
            data:data,
            //  添加token参数
            headers:{
                'Authorization': 'Bearer ' + this.user.accessToken
            }
        })
            .then(result => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:'success',
                    data:result
                });

                if(that.vue && false){
                    that.vue.$aShow(result.data.msg);
                }
            })
            .catch(e => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:"error",
                    data:e
                });

                if(that.vue){
                    that.vue.$aShow(e);
                }
            });
    }

    post(url,data,eventName){
        let that = this;
        //  这里会自动生成时间戳 和 对表单里面的内容进行签名
        let timestamp = new Date().valueOf()+"";
        let sign = md5.hexMD5(JSON.stringify(data)+timestamp);
        axios.request({
            url:url,
            method:"POST",
            data:data,
            headers:{
                timestamp:timestamp,
                sign:sign
            }
        })
            .then(result => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:'success',
                    data:result
                });

                if(that.vue && false){
                    that.vue.$aShow(result.data.msg);
                }
            })
            .catch(e => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:"error",
                    data:e
                });

                if(that.vue){
                    that.vue.$aShow(e);
                }
            });
    }

    //  登录特殊处理
    login(url,data,eventName){
        let data1 = {};
        //  复制一份数据
        Object.assign(data1,data);
        //  这里会自动生成时间戳 和 对表单里面的内容进行签名
        let timestamp = new Date().valueOf()+"";
        let pwd = md5.hexMD5(data1.password+timestamp);

        let that = this;

        data1.password = pwd;
        let sign = md5.hexMD5(JSON.stringify(data1)+timestamp);
        axios.request({
            url:url.startsWith("http") ? url : this.host+url,
            method:"POST",
            data:data1,
            headers:{
                timestamp:timestamp,
                sign:sign
            }
        })
            .then(result => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:'success',
                    data:result
                });

                if(that.vue && false){
                    that.vue.$aShow(result.data.msg);
                }
            })
            .catch(e => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:"error",
                    data:e
                });

                if(that.vue){
                    that.vue.$aShow(e);
                }
            });
    }

    postWithToken(url,data,eventName){
        //  这里会自动生成时间戳 和 对表单里面的内容进行签名
        let timestamp = new Date().valueOf()+"";
        let sign = md5.hexMD5(JSON.stringify(data)+timestamp);

        let that = this;
        if(this.user == null || this.user.accessToken == null){
            eventLoop.publishEvent({
                type:eventName,
                code:-3,
                msg:"用户未登录",
                data:{}
            });
            return ;
        }

        axios.request({
            url:this.host+url,
            method:"POST",
            data:data,
            //  添加token信息
            headers:{
                'Authorization': 'Bearer ' + this.user.accessToken,
                'timestamp': timestamp,
                'sign': sign
            }
        })
            .then(result => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:'success',
                    data:result
                });

                if(that.vue && false){
                    that.vue.$aShow(result.data.msg);
                }
            })
            .catch(e => {
                eventLoop.publishEvent({
                    type:eventName,
                    status:"error",
                    data:e
                });

                if(that.vue){
                    that.vue.$aShow(e);
                }
            });
    }
}

let ajax = new Ajax();

export default ajax;